package northwind.web;

import java.util.Map;
import northwind.model.Category;
import northwind.service.ProductService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.support.SessionStatus;

/**
 * Handles requests for the application home page.
 */
@Controller
public class ProductsController {

    private static final Logger logger = LoggerFactory
            .getLogger(ProductsController.class);

    private final ProductService productService;

    @Autowired
    public ProductsController(ProductService productService) {
        super();
        this.productService = productService;
    }

    @RequestMapping(value = "/category/new", method = RequestMethod.GET)
    public String initCreationForm(Map<String, Object> model) {
        Category category = new Category();
        model.put("category", category);
        return "createOrUpdateCategoryForm";
    }

    @RequestMapping(value = "/category/new", method = RequestMethod.POST)
    public String processCreationForm(Category category, BindingResult result, SessionStatus status) {
        System.out.println("Category: " + category);
        this.productService.createCategory(category.getCategoryName(), category.getDescription());
        status.setComplete();
        return "owners/createOrUpdateOwnerForm";
    }

}
